var objXML;
var link_id = "";
var XMLLock = 0;
var XMLStackDiv = ["", "", "", "", ""];
var XMLStackURL = ["", "", "", "", ""];
var XMLStackCDv = [0, 0, 0, 0, 0];
var XMLStackSize = 0;
var XMLFlag = 1; //Alexey; 1 - enable loading, 0 disable.

function loadXMLFile(div_id, xml_file, clearDiv) {
/*  if (XMLFlag == 0)
    return;
  XMLFlag = 0;*/
  //alert("Before Adding - " + XMLStackSize + div_id);
        if (div_id != "") { //add an element
                XMLStackDiv[XMLStackSize] = div_id;
                XMLStackURL[XMLStackSize] = xml_file;
                XMLStackCDv[XMLStackSize] = clearDiv;
                XMLStackSize++;
        }
  //alert("After Adding - " + XMLStackSize);
        //next element
        if (XMLStackSize > 0 && XMLLock == 0) { //are there any?
                XMLLock = 1;
                // very rulezzz window
                //window.open(XMLStackURL[0], "as", "");
                //window.open(XMLStackURL[0]);
                //alert('Check XML');
                link_id = XMLStackDiv[0];
                //alert ("Loading " + link_id + " - " + xml_file);
                if (XMLStackCDv[0] == 1) document.getElementById("i" + link_id).innerHTML = "&nbsp;" + LoadingText;
                objXML = jsXML.createDOMDocument();
                objXML.onreadystatechange = doLoad_XMLFile;
                objXML.load(XMLStackURL[0]);
        }
}

function doLoad_XMLFile() {
  if (objXML.readyState == 4) {
    RemoveFromStack();
    var cDiv = document.getElementById("i" + link_id);
    var root = objXML.documentElement;
    //alert(objXML.xml);
    if (objXML.parseError == 0 && root != null) {
      var innerHtmlString = "";
      var id = "";
      var name = "";
      var subHref = "";
      //alert (root.tagName);
      switch (root.tagName) {
        case "Tree":
          //alert("!!!!");
          fld_visible_temp = root.getAttribute("Fld_Visible");
          if (fld_visible_temp == "%FLD_VIS%") fld_visible_temp = ""; else fld_visible = fld_visible_temp;
          class_operations_temp = root.getAttribute("Class_Operations");
          if (class_operations_temp == "%CLASSOPERATIONS%") class_operations = ""; else class_operations = class_operations_temp;
          cDiv.innerHTML = "";
          GetChildren (objXML.documentElement.childNodes, cDiv, 1);
          cDiv.isLoaded = "1";
          cDiv.style.display = "block";
          if (selectedItem == "") MoveLeft();
          break;
        case "Info":
          cDiv.innerHTML = "";
          var newName = objXML.documentElement.getAttribute("Name");
	  var var_quan = objXML.documentElement.getAttribute("var_quan");
	  if (window.setQuan) {
	  	setQuan (var_quan);
	  }
          if (newName != "" && newName != "%NAME%") RenameItem(newName);
          var newImg = objXML.documentElement.getAttribute("NewImg");
          if (newImg != null && newImg != "" && newImg != "%IMG%") {
            cEl = document.getElementById("pi" + selectedItem);
            cEl.src = imgroot + "/" + newImg + ".png";
          }
          var newID = objXML.documentElement.getAttribute("NewID");
          if (newID != null && newID != "" && newID != "%ID%") ReIDItem(newID);
          var newDatHref = objXML.documentElement.getAttribute("NewDatHref");
          if (newDatHref != null && newDatHref != "" && newDatHref != "%DatHref%") {
            cEl = document.getElementById("i" + selectedItem);
            cEl.datHref = newDatHref;
          }
          var children = objXML.documentElement.childNodes;
          //alert(children.length);
          for (i = 0; i < children.length; i++) {
            if (children[i].tagName != null) {
              id = children[i].getAttribute("GroupId");
              //alert(id);
              name = children[i].getAttribute("Title");
              subHref = children[i].getAttribute("NodeXmlSrc");
              innerHtmlString = "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"5\"><tr><td colspan=\"2\"><b>" + name +
                "</b></td></tr>";
              var items = children[i].childNodes;
              for (j = 0; j < items.length; j++) {
                if (items[j].tagName != null) {
                	if (items[j].tagName == "script") {
                		eval (items[j].getAttribute ("exec"));
                		continue;
                	}
                  item_id = items[j].getAttribute("NodeId");
                  item_name = replaceSpecChar(items[j].getAttribute("Title"));
                  item_type = items[j].getAttribute("Type");
                         addition_parameters = items[j].getAttribute("Additional");
                        if (addition_parameters == "%ADDITIONAL%" || addition_parameters == null) addition_parameters = "";
                        //alert(addition_parameters);

                  innerHtmlString += "<tr><td width=\"0\"><span style=\"{ white-space : nowrap;}\">" + item_name +
                      "</span></td><td width=\"100%\" nowrap>";
                  switch (item_type) {
                    case "Label":
                      innerHtmlString += items[j].getAttribute("Value");
                      break;
                    case "Text":
                      item_value = replaceSpecChar(items[j].getAttribute("Value"));
                      innerHtmlString += "<input id=\"" + item_id +
                      "\" type=\"text\" size=\"70\" value=\"" + item_value +
                      "\" " + addition_parameters + " />";
                      break;
                    case "CheckBox":
                      item_value = replaceSpecChar(items[j].getAttribute("Value"));
                      innerHtmlString += "<input id=\"" + item_id +
                      "\" type=\"checkbox\""; // value=\"" + item_value +
                      if (item_value == 1) innerHtmlString += "checked";
                      innerHtmlString += " " + addition_parameters + " />";
                      //"\" type=\"checkbox\" value=\"" + item_value +
                      //"\" checked />";
                      break;
                    case "Hidden":
                      item_value = replaceSpecChar(items[j].getAttribute("Value"));
                      innerHtmlString += "<input id=\"" + item_id +
                      "\" type=\"hidden\" value=\"" + item_value +
                      "\" />";
                      break;
                    case "Password":
                      item_value = replaceSpecChar(items[j].getAttribute("Value"));
                      innerHtmlString += "<input id=\"" + item_id +
                      "\" type=\"password\" value=\"" + item_value +
                      "\" />";
                      break;
                    case "Button":
                      //alert(fld_visible);
                      item_value = replaceSpecChar(items[j].getAttribute("Value"));
                      innerHtmlString += "<button onclick=\"javascript:ProcessButton([" + item_value +
                      "]);\" " + addition_parameters + ">" + item_name +
                      "</button>";
                      break;
                    case "Link":
                      item_value = replaceSpecChar(items[j].getAttribute("Value"));
                      innerHtmlString += "<a href=\"" + item_value +
                      "\">" + item_name +
                      "</a>";
                      break;
                    case "Option":
                      innerHtmlString += "<select id=\"" + item_id +
                      "\" " + addition_parameters + ">";
                      var opts = items[j].childNodes;
                      for (k = 0; k < opts.length; k++) {
                        if (opts[k].tagName != null) {
                          opt_id = opts[k].getAttribute("OptionID");
                          opt_title = opts[k].getAttribute("Title");
                          opt_sel = opts[k].getAttribute("Selected");
                          innerHtmlString += "<option value=\"" + opt_id + "\"";
                          if (opt_sel == "selected") innerHtmlString += " selected";
                          innerHtmlString += ">" + opt_title + "</option>";
                         }
                      }
                      innerHtmlString += "</select>";
                      break;
                    case "ListBox":
                      innerHtmlString += "<select id=\"" + item_id + "\" MULTIPLE " + addition_parameters + ">";
                      var opts = items[j].childNodes;
                      for (k = 0; k < opts.length; k++) {
                        if (opts[k].tagName != null) {
                          opt_id = opts[k].getAttribute("OptionID");
                          opt_title = opts[k].getAttribute("Title");
                          opt_sel = opts[k].getAttribute("Selected");
                          innerHtmlString += "<option value=\"" + opt_id + "\"";
                          if (opt_sel == "selected") innerHtmlString += " selected";
                          innerHtmlString += ">" + opt_title + "</option>";
                         }
                      }
                      innerHtmlString += "<option value='0'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</Option>";
                      innerHtmlString += "</select>";
                      break;
                  }
                  innerHtmlString +="</td></tr>";
                }
              }
              innerHtmlString += "</table>";
              var oDiv = document.getElementById("info_" + id);
              if (oDiv == null) {
                oDiv = document.createElement("DIV");
                oDiv.id = "info_" + id;
                oDiv.innerHTML = innerHtmlString;
                cDiv.appendChild(oDiv);
              }
              else {
                oDiv.innerHTML = innerHtmlString;
              }
            }
          }
          break;
        case "Action":
                  var ActionId = objXML.documentElement.getAttribute("ActionId");
                  if (objXML.documentElement.getAttribute("Success") != 1) break;
                  var id = objXML.documentElement.getAttribute("Parent");
                  //alert(id);
                  var sel_id = objXML.documentElement.getAttribute("Select");
                  //alert(objXML.documentElement.getAttribute("Select"));
                  var childNodes = objXML.documentElement.childNodes;
      var cDiv = document.getElementById("i" + id);
                   switch (ActionId) {
                        case "Add":
                                if (id != "root") {
                                        var NodeXmlSrc = cDiv.getAttribute("subHref"); if (moz) NodeXmlSrc = cDiv.subHref;
                                        if (NodeXmlSrc == null || NodeXmlSrc == "") {//item is not expandable yet
                                                //alert("item is not expandable yet");
                                                var tAttr = document.createAttribute("subHref");
                                                cDiv.setAttributeNode(tAttr);
                                                cDiv.subHref = objXML.documentElement.getAttribute("NodeXmlSrc") + "&sel=0";
                                                if (ie)
                                                        document.getElementById("p" + id).attachEvent("onclick", expandCollapse);
                                                if (moz)
                                                        document.getElementById("p" + id).addEventListener("click", expandCollapse, false);
                                                expandCollapseItem(id);
                                        }
                                        else if (cDiv.isLoaded == "0") {//item is expandable but not loaded
                                                //alert("item is expandable but not loaded");
                                                var div_id = GetChildren(childNodes, cDiv, 0);
                                                cDiv.subHref = cDiv.subHref.concat("&sel=", div_id);
                                                expandCollapseItem(id);
                                        }
                                        else {//item is loaded
                                                //alert("item is loaded");
                                                var div_id = GetChildren(childNodes, cDiv, 0);
                                                if (cDiv.style.display == "block") expandCollapseItem(id);
                                                cDiv.isLoaded = "0";
                                                //alert (div_id);
                                                cDiv.subHref = cDiv.subHref.concat("&sel=", div_id);
                                                expandCollapseItem(id);
                                                //selectItem(div_id);
                                        }
                var newName = objXML.documentElement.getAttribute("Name");
                if (newName != "" && newName != "%NAME%") RenameItem(newName);
                                  if (sel_id!= null && sel_id!="" && sel_id!="%SELECT%") {
                                    //alert(sel_id);
                                    XMLLock = 0;
                                    selectItem(sel_id);
                                    XMLLock = 1;
                                  }
                                } else {
                                        //alert("root");
                                        selectItem(GetChildren(objXML.documentElement.childNodes, cDiv, 1));
                                }
                                break;
                        case "Delete":
                                MoveUp();
                    cDiv = document.getElementById(id);
                    //alert(id);
                                pDiv = cDiv.parentNode;
                                ppDiv = pDiv.parentNode;
                                if (ppDiv.id != null && ppDiv.id != "" && ppDiv.id != "iroot") ippDiv = document.getElementById("i" + ppDiv.id);
                                else ippDiv = null;
                                pDiv.removeChild(cDiv);
                                //alert(pDiv.id);
                                if (pDiv.childNodes.length == 0) { //no more children left
                                        if (pDiv.id == 'iroot') {
                                                selectedItem = "";
                                                document.getElementById("info").innerHTML = "&nbsp;" + UnavailableText;
                                        }
                                        else {
                                                pDiv.subHref = "";
                                                pDiv.isLoaded = 0;
                                                pDiv.style.display = "none";
                                            document.getElementById("p" + ppDiv.id).src = imgroot + "/Empty.gif";
                                                if (ie)
                                                        document.getElementById("p" + ppDiv.id).detachEvent("onclick", expandCollapse);
                                                if (moz)
                                                        document.getElementById("p" + ppDiv.id).removeEventListener("click", expandCollapse, false);
                                        }
                                }
                                else {
                                        //alert(ippDiv);
                                        if (ippDiv != null) {
                                                expandCollapseItem(ppDiv.id);
                                                ippDiv.isLoaded = "0";
                                                expandCollapseItem(ppDiv.id);
                                        }
                                        if (ppDiv.id == "iroot") {
                                                //
                                        }
                                }
                                if (sel_id!= null && sel_id!="" && sel_id!="%SELECT%") {
                                  //alert(sel_id);
                                  XMLLock = 0;
                                  selectItem(sel_id);
                                  XMLLock = 1;
                                }
                                break;
                  case "Select":
                                  XMLLock = 0;
                                  selectItem(sel_id);
                                  XMLLock = 1;
                  break;

        }
                  break;
      }
    }
    else {
      //alert(objXML.xml);
      //alert("XML Error. Describe the situation to the administrator.");
      cDiv.innerHTML = "&nbsp;" + UnavailableText;
    }
    XMLLock = 0;
    loadXMLFile("", "", 0);
  }
}

function RenameItem (name) {
  //alert(selectedItem + " - " + name);
  var cHref = document.getElementById("a" + selectedItem);
  cHref.innerHTML = replaceSpecChar(name);
}

function ReIDItem (ID) {
  //alert(ID);
  var cEl = document.getElementById(selectedItem);
  cEl.id = ID;
  cEl = document.getElementById("a" + selectedItem);
  cEl.id = "a" + ID;
  cEl.href = "javascript:selectItem('" + ID + "');";
  cEl = document.getElementById("p" + selectedItem);
  cEl.id = "p" + ID;
  cEl = document.getElementById("s" + selectedItem);
  cEl.id = "s" + ID;
  cEl = document.getElementById("i" + selectedItem);
  cEl.id = "i" + ID;
  selectedItem = ID;
}

function RemoveFromStack () {
        for (i = 0; i <= XMLStackSize && i <= 4; i++) {  //remove from the stack
                XMLStackDiv[i] = XMLStackDiv[i + 1];
                XMLStackURL[i] = XMLStackURL[i + 1];
                XMLStackCDv[i] = XMLStackCDv[i + 1];
        }
        XMLStackSize--;
        //alert("After Deleting - " + XMLStackSize);
}


function GetChildren (children, cDiv, createDiv) {
        id = "";
        for (i = 0; i < children.length; i++) {
                if (children[i].tagName != null) {
                        id = children[i].getAttribute("NodeId");
                        if (createDiv == 0) return id;
                        name = replaceSpecChar(children[i].getAttribute("Title"));
                        if (children[i].getAttribute("ImgType") != null && children[i].getAttribute("ImgType") != "%IMG_TYPE%")
                          icon = "\"><img id=\"pi" + id + "\" src=\""  + imgroot + "/" + children[i].getAttribute("ImgType") + ".png";
                        else icon = "";
                        if (children[i].getAttribute("Href") != null) datHref = children[i].getAttribute("Href");
                        else datHref = "";
                        //alert("datHref = " + datHref);
                        if (children[i].getAttribute("Hsel") != null && children[i].getAttribute("Hsel") != "%CLASS_HS%") datSelect = children[i].getAttribute("Hsel");
                        else datSelect = "";
                        //alert(id + " - " + name + " - " + datSelect);
                        if (children[i].getAttribute("NodeXmlSrc") != null && children[i].getAttribute("NodeXmlSrc") != "") {
                                subHref = children[i].getAttribute("NodeXmlSrc");
                                imgName = "plus.gif";
                        }
                        else {
                                subHref = "";
                                imgName = "Empty.gif"; //dot.gif
                        }
                        innerHtmlString = "<img id=\"p" + id +
                                "\" src=\"" + imgroot + "/" + imgName + icon +
                                "\"><span id=\"s" + id +
                                "\" class=\"notSelected\"><a id=\"a" + id +
                                "\" title=\"" + name +
                                "\" href=\"javascript:selectItem('" + id +
                                "');\">" + name +
                                "</a></span><div id=\"i" + id +
                                "\"></div>";

                        var oDiv = document.createElement("DIV");
                        oDiv.id = id;
                        oDiv.innerHTML = innerHtmlString;
                        cDiv.appendChild(oDiv);
                        tDiv = document.getElementById("i" + id);
                        tDiv.style.display = "none";
                        tDiv.style.paddingLeft = "16px";
                        var oAttr = document.createAttribute("isLoaded");
                        tDiv.setAttributeNode(oAttr);
                        tDiv.isLoaded = 0;
                        if (datSelect != "") {
                                var tAttr = document.createAttribute("datSelect");
                                tDiv.setAttributeNode(tAttr);
                                tDiv.datSelect = datSelect;
                        }
                        var tAttr = document.createAttribute("subHref");
                        tDiv.setAttributeNode(tAttr);
                        tDiv.subHref = subHref;
                        var sAttr = document.createAttribute("datHref");
                        tDiv.setAttributeNode(sAttr);
                        tDiv.datHref = datHref;
                        if (subHref != "") {
                                if (ie)
                                        document.getElementById("p" + id).attachEvent("onclick", expandCollapse);
                                if (moz)
                                        document.getElementById("p" + id).addEventListener("click", expandCollapse, false);
                        }//if

                        var act = children[i].getAttribute("Act");
                        if (act != null && act.substring(0,6) == "select") {
                          act = act.substring(6);
                          //alert("select"+ id);
                                XMLLock = 0;
                                selectItem(id);
                                XMLLock = 1;
                        }
                        if (act != null && act.substring(0,6) == "expand") {
                          //alert("expand" + id);
                                XMLLock = 0;
                                expandCollapseItem_Par(id, "dest_id=" + act.substring(6));
                                //alert(children[i].getAttribute("Act").substring(6));
                                XMLLock = 1;
                        }

                }//if
        }//for
        return id;
}

function replaceSpecChar(ttt)  {
        ttt = ttt.replace(/&/g, "&amp;");
        ttt = ttt.replace(/"/g, "&quot;");
        ttt = ttt.replace(/'/g, "&#039;");
        ttt = ttt.replace(/</g, "&lt;");
        ttt = ttt.replace(/>/g, "&gt;");
        return ttt;
}